package com.bjdd.jdy.ui.order;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.adapter.TakeOrderAdapter;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.OrderBean;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.view.other.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicDefaultFooter;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class OrderActivity extends BaseActivity<OrderPresenter, OrderModel> implements OrderContract.View {

    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.rcv)
    RecyclerView rcv;
    @Bind(R.id.swiperefreshlayout)
    PtrFrameLayout refreshlayout;
    private TakeOrderAdapter adapter;
    private List<OrderBean> orders;
    private final int size = 10;
    private int page = 0;
    private boolean isLoadMore = false;
    @Override
    public int getLayoutId() {
        return R.layout.activity_order;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("我的订单");
        initPtrFrameLayout();
        rcv.setLayoutManager(new LinearLayoutManager(context));
        rcv.addItemDecoration(new DividerItemDecoration(context,
                DividerItemDecoration.VERTICAL_LIST));
        orders = new ArrayList<>();
        adapter = new TakeOrderAdapter(context,orders);
        rcv.setAdapter(adapter);
        refreshlayout.autoRefresh();
        //presenter.getOrders(page,size);
    }
    private void initPtrFrameLayout(){

        PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(context);
        header.onUIRefreshBegin(refreshlayout);
        refreshlayout.setHeaderView(header);
        refreshlayout.addPtrUIHandler(header);
        PtrClassicDefaultFooter footer = new PtrClassicDefaultFooter(context);
        footer.onUIRefreshBegin(refreshlayout);
        refreshlayout.setFooterView(footer);
        refreshlayout.addPtrUIHandler(footer);
        refreshlayout.setLoadingMinTime(1000);
        refreshlayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                isLoadMore = true;
                page ++;
                presenter.getOrders(page,size);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isLoadMore = false;
                page = 0;
                presenter.getOrders(page,size);

            }
        });
    }

    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void orderSuccess(List<OrderBean> data) {
        if (isLoadMore){
            orders.addAll(data);
        }else {
            orders.clear();
            orders.addAll(data);
        }
        adapter.notifyDataSetChanged();
        refreshlayout.refreshComplete();
    }
}
